﻿/**
 * The prototype of khmer keyboard that can be use for web applications.
 * @author  pykhmer
 * @Email   admin@pykhmer.org
 * @returns KhmerKey}
 * @since   29/05/2011
 * @version 1.0 beta
 * @licence Free Open source
 */
function KhmerKey()
{
	// Defined general properties
	var output = "";
	this.capsState = 0;
	this.langState = 1; //0 = English, 1 = Khmer
	this.shiftState = 0;
	/**
	 *@desc EngToKhmer method to change the English key to correspod khmer key  
	 */
	this.EngToKhmer = function (str) {
				st = "";
				switch (str) {
					case 1:   (this.capsState  == 0) ? st = "«" : st = "»"; break;
					case 2:   (this.capsState  == 0) ? st = "១" : st = "!"; break;
					case 3:   (this.capsState  == 0) ? st = "២" : st = "ៗ"; break; 
					case 4:   (this.capsState  == 0) ? st = "៣" : st = "ឦ"; break;
					case 5:   (this.capsState  == 0) ? st = "៤" : st = '៛'; break;
					case 6:   (this.capsState  == 0) ? st = "៥" : st = "%"; break;
					case 7:   (this.capsState  == 0) ? st = "៦" : st = "៍"; break;
					case 8:   (this.capsState  == 0) ? st = "៧" : st = "័"; break;
					case 9:   (this.capsState  == 0) ? st = "៨" : st = "៏"; break;
					case 10:  (this.capsState  == 0) ? st = "៩" : st = "ឳ"; break;
					case 11:  (this.capsState  == 0) ? st = "ឩ" : st = "ឱ"; break;
					case 12:  (this.capsState  == 0) ? st = "ឥ" : st = "៌"; break;
					case 13:  (this.capsState  == 0) ? st = "ឲ" : st = "ឰ"; break;
					
					case 16:  (this.capsState  == 0) ? st = "ឆ" : st = "ឈ"; break;
					case 17:  (this.capsState  == 0) ? st = "ឺ" : st =  "ឺ"; break;
					case 18:  (this.capsState  == 0) ? st = "េ" : st = "ែ"; break;
					case 19:  (this.capsState  == 0) ? st = "រ" : st = "ឬ"; break;
					case 20:  (this.capsState  == 0) ? st = "ត" : st = "ទ"; break;
					case 21:  (this.capsState  == 0) ? st = "យ" : st = "ួ"; break;
					case 22:  (this.capsState  == 0) ? st = "ុ" : st = "ូ"; break;
					case 23:  (this.capsState  == 0) ? st = "ិ" : st = "ី"; break;
					case 24:  (this.capsState  == 0) ? st = "ោ" : st = "ៅ"; break;
					case 25:  (this.capsState  == 0) ? st = "ផ" : st = "ភ"; break;
					case 26:  (this.capsState  == 0) ? st = "ៀ" : st = "ឿ"; break;
					case 27:  (this.capsState  == 0) ? st = "ឪ" : st = "ឧ"; break;
					case 28:  (this.capsState  == 0) ? st = "ឮ" : st = "ឭ"; break;
					case 30:  (this.capsState  == 0) ? st = "ា" : st = "ាំ"; break;
					case 31:  (this.capsState  == 0) ? st = "ស" : st = "ៃ"; break;
					case 32:  (this.capsState  == 0) ? st = "ដ" : st = "ឌ"; break;
					case 33:  (this.capsState  == 0) ? st = "ថ" : st = "ធ"; break;
					case 34:  (this.capsState  == 0) ? st = "ង" : st = "អ"; break;
					case 35:  (this.capsState  == 0) ? st = "ហ" : st = "ះ"; break;
					case 36:  (this.capsState  == 0) ? st = "្" : st = "ញ"; break;
					case 37:  (this.capsState  == 0) ? st = "ក" : st = "គ"; break;
					case 38:  (this.capsState  == 0) ? st = "ល" : st = "ឡ"; break;
					case 39:  (this.capsState  == 0) ? st = "ើ" : st = "ោះ"; break;
					case 40:  (this.capsState  == 0) ? st = "់" : st = "៉"; break;
					case 41:  (this.capsState  == 0) ? st = "\n" : st = "\n"; break; // Enter
					case 43:  (this.capsState  == 0) ? st = "ឋ" : st = "ឍ"; break;
					case 44:  (this.capsState  == 0) ? st = "ខ" : st = "ឃ"; break;
					case 45:  (this.capsState  == 0) ? st = "ច" : st = "ជ"; break;
					case 46:  (this.capsState  == 0) ? st = "វ" : st = "េះ"; break;
					case 47:  (this.capsState  == 0) ? st = "ប" : st = "ព"; break;
					case 48:  (this.capsState  == 0) ? st = "ន" : st = "ណ"; break;
					case 49:  (this.capsState  == 0) ? st = "ម" : st = "ំ"; break;
					case 50:  (this.capsState  == 0) ? st = "ុំ" : st = "ុះ"; break;
					case 51:  (this.capsState  == 0) ? st = "។" : st = "៕"; break;
					case 52:  (this.capsState  == 0) ? st = "៊" : st = "ឯ"; break;
					case 57:   st = " "; break;
				}
				return st;
	};// end EngToKhmer()
	/**
	 * 
	 */		
	this.EngToEng = function (str) {
			st = "";
			switch (str) {
				case 1:   (this.capsState  == 0) ? st = "`" : st = "~"; break;
				case 2:   (this.capsState  == 0) ? st = "1" : st = "!"; break;
				case 3:   (this.capsState  == 0) ? st = "2" : st = "@"; break;
				case 4:   (this.capsState  == 0) ? st = "3" : st = "#"; break;
				case 5:   (this.capsState  == 0) ? st = "4" : st = "$"; break;
				case 6:   (this.capsState  == 0) ? st = "5" : st = "%"; break;
				case 7:   (this.capsState  == 0) ? st = "6" : st = "^"; break;
				case 8:   (this.capsState  == 0) ? st = "7" : st = "&"; break;
				case 9:   (this.capsState  == 0) ? st = "8" : st = "*"; break;
				case 10:  (this.capsState  == 0) ? st = "9" : st = "("; break;
				case 11:  (this.capsState  == 0) ? st = "0" : st = ")"; break;
				case 12:  (this.capsState  == 0) ? st = "-" : st = "_"; break;
				case 13:  (this.capsState  == 0) ? st = "=" : st = "+"; break;
				case 16:  (this.capsState  == 0) ? st = "q" : st = "Q"; break;
				case 17:  (this.capsState  == 0) ? st = "w" : st = "W"; break;
				case 18:  (this.capsState  == 0) ? st = "e" : st = "E"; break;
				case 19:  (this.capsState  == 0) ? st = "r" : st = "R"; break;
				case 20:  (this.capsState  == 0) ? st = "t" : st = "T"; break;
				case 21:  (this.capsState  == 0) ? st = "y" : st = "Y"; break;
				case 22:  (this.capsState  == 0) ? st = "u" : st = "U"; break;
				case 23:  (this.capsState  == 0) ? st = "i" : st = "I"; break;
				case 24:  (this.capsState  == 0) ? st = "o" : st = "O"; break;
				case 25:  (this.capsState  == 0) ? st = "p" : st = "P"; break;
				case 26:  (this.capsState  == 0) ? st = "[" : st = "{"; break;
				case 27:  (this.capsState  == 0) ? st = "]" : st = "}"; break;
				case 28:  (this.capsState  == 0) ? st = "\\" : st = "|"; break;
				case 30:  (this.capsState  == 0) ? st = "a" : st = "A"; break;
				case 31:  (this.capsState  == 0) ? st = "s" : st = "S"; break;
				case 32:  (this.capsState  == 0) ? st = "d" : st = "D"; break;
				case 33:  (this.capsState  == 0) ? st = "f" : st = "F"; break;
				case 34:  (this.capsState  == 0) ? st = "g" : st = "G"; break;
				case 35:  (this.capsState  == 0) ? st = "h" : st = "H"; break;
				case 36:  (this.capsState  == 0) ? st = "j" : st = "J"; break;
				case 37:  (this.capsState  == 0) ? st = "k" : st = "K"; break;
				case 38:  (this.capsState  == 0) ? st = "l" : st = "L"; break;
				case 39:  (this.capsState  == 0) ? st = ";" : st = ":"; break;
				case 40:  (this.capsState  == 0) ? st = "'" : st = ""; break;
				case 43:  (this.capsState  == 0) ? st = "z" : st = "Z"; break;
				case 44:  (this.capsState  == 0) ? st = "x" : st = "X"; break;
				case 45:  (this.capsState  == 0) ? st = "c" : st = "C"; break;
				case 46:  (this.capsState  == 0) ? st = "v" : st = "V"; break;
				case 47:  (this.capsState  == 0) ? st = "b" : st = "B"; break;
				case 48:  (this.capsState  == 0) ? st = "n" : st = "N"; break;
				case 49:  (this.capsState  == 0) ? st = "m" : st = "M"; break;
				case 50:  (this.capsState  == 0) ? st = "," : st = "<"; break;
				case 51:  (this.capsState  == 0) ? st = "." : st = ">"; break;
				case 52:  (this.capsState  == 0) ? st = "/" : st = "?"; break;
			}
			return st;
	}; // end EngToEng()


	/**
	 * @desc keyPress functionto control user press keyboard 
	 * TODO method: the visual keyboard that I will do next.
	 */
	this.keyPress = function (str) {
		output = document.result.TextDisplay.value;
		st = "";
		switch (str) {
			case 14:  (this.capsState  == 0) ? st = "" : st = ""; 
				output = output.substring(0, output.length - 1);
				break; // backspace
			case 15:  (this.capsState  == 0) ? st = "\t" : st = "\t"; break; // tab
			case 29:  (this.capsState  == 0) ? this.capsState = 1 : this.capsState = 0; break; //capslock
			case 41:  (this.capsState  == 0) ? st = "\n" : st = "\n"; break; // Enter
			case 42:  (this.capsState  == 0) ? this.capsState = 1 : this.capsState = 0; break; // Left Shift
			case 53:  (this.capsState  == 0) ? this.capsState = 1 : this.capsState = 0; break; // Right Shift
			case 54:  (this.capsState  == 0) ? st = "" : st = ""; break; // Left Ctrl
			case 55:  (this.capsState  == 0) ? st = "" : st = ""; break; // Left win
			case 56:  (this.capsState  == 0) ? st = "" : st = ""; break; // Left Alt
			case 57:  (this.capsState  == 0) ? st = " " : st = " "; break; // Spacebar
			case 58:  (this.capsState  == 0) ? st = "" : st = ""; break; // Right Alt
			case 59:  (this.capsState  == 0) ? st = "" : st = ""; break; // Menu
			case 60:  (this.capsState  == 0) ? st = "" : st = ""; break; // Right Ctrl
			case 61:  (this.capsState  == 0) ? st = "" : st = ""; break; // Esc
			case 62:  (this.capsState  == 0) ? st = "" : st = ""; break; // F1
			case 63:  (this.capsState  == 0) ? st = "" : st = ""; break; // F2
			case 64:  (this.capsState  == 0) ? st = "" : st = ""; break; // F3
			case 65:  (this.capsState  == 0) ? st = "" : st = ""; break; // F4
			case 66:  (this.capsState  == 0) ? st = "" : st = ""; break; // F5
			case 67:  (this.capsState  == 0) ? st = "" : st = ""; break; // F6
			case 68:  (this.capsState  == 0) ? st = "" : st = ""; break; // F7
			case 69:  (this.capsState  == 0) ? st = "" : st = ""; break; // F8
			case 70:  (this.capsState  == 0) ? st = "" : st = ""; break; // F9
			case 71:  (this.capsState  == 0) ? st = "" : st = ""; break; // F10
			case 72:  (this.capsState  == 0) ? st = "" : st = ""; break; // F11
			case 73:  (this.capsState  == 0) ? st = "" : st = ""; break; // F12 
			default:
					  (this.langState == 0) ? st = this.EngToEng(str) : st = this.EngToKhmer(str);
		}   
		output = output + st;
		document.result.TextDisplay.value = output;
	}; // end keyPress()
	/**
	 * 
	 */
	this.CharToCode = function (str) {
		code = 42; // lshift
		this.shiftState = 1;
		switch (str) {
			case "1": this.shiftState = 0; case "!": code = 2; break;
			case "2": this.shiftState = 0; case "@": code = 3; break;
			case "3": this.shiftState = 0; case "#": code = 4; break;
			case "4": this.shiftState = 0; case "$": code = 5; break;
			case "5": this.shiftState = 0; case "%": code = 6; break;
			case "6": this.shiftState = 0; case "^": code = 7; break;
			case "7": this.shiftState = 0; case "&": code = 8; break;
			case "8": this.shiftState = 0; case "*": code = 9; break;
			case "9": this.shiftState = 0; case "(": code = 10; break;
			case "0": this.shiftState = 0; case ")": code = 11; break;
			case "-": this.shiftState = 0; case "_": code = 12; break;
			case "=": this.shiftState = 0; case "+": code = 13; break;
			case "q": this.shiftState = 0; case "Q": code = 16; break;
			case "w": this.shiftState = 0; case "W": code = 17; break;
			case "e": this.shiftState = 0; case "E": code = 18; break;
			case "r": this.shiftState = 0; case "R": code = 19; break;
			case "t": this.shiftState = 0; case "T": code = 20; break;
			case "y": this.shiftState = 0; case "Y": code = 21; break;
			case "u": this.shiftState = 0; case "U": code = 22; break;
			case "i": this.shiftState = 0; case "I": code = 23; break;
			case "o": this.shiftState = 0; case "O": code = 24; break;
			case "p": this.shiftState = 0; case "P": code = 25; break;
			case "[": this.shiftState = 0; case "{": code = 26; break;
			case "]": this.shiftState = 0; case "}": code = 27; break;
			case "\\": this.shiftState = 0; case "|": code = 28; break;
			case "a": this.shiftState = 0; case "A": code = 30; break;
			case "s": this.shiftState = 0; case "S": code = 31; break;
			case "d": this.shiftState = 0; case "D": code = 32; break;
			case "f": this.shiftState = 0; case "F": code = 33; break;
			case "g": this.shiftState = 0; case "G": code = 34; break;
			case "h": this.shiftState = 0; case "H": code = 35; break;
			case "j": this.shiftState = 0; case "J": code = 36; break;
			case "k": this.shiftState = 0; case "K": code = 37; break;
			case "l": this.shiftState = 0; case "L": code = 38; break;
			case ";": this.shiftState = 0; case ":": code = 39; break;
			case "'": this.shiftState = 0; case "\"": code = 40; break;
			case "\n": this.shiftState = 0; case "\n": code = 41; break; // Enter Key
			case "z": this.shiftState = 0; case "Z": code = 43; break;
			case "x": this.shiftState = 0; case "X": code = 44; break;
			case "c": this.shiftState = 0; case "C": code = 45; break;
			case "v": this.shiftState = 0; case "V": code = 46; break;
			case "b": this.shiftState = 0; case "B": code = 47; break;
			case "n": this.shiftState = 0; case "N": code = 48; break;
			case "m": this.shiftState = 0; case "M": code = 49; break;
			case ",": this.shiftState = 0; case "<": code = 50; break;
			case ".": this.shiftState = 0; case ">": code = 51; break;
			case "/": this.shiftState = 0; case "?": code = 52; break;
			case " ": this.shiftState = 0; code = 57; break;
		}
		return code;
	}; //end CharToCode()
	/**
	 * 
	 */
	this.keyKhmer = function (elementID) {
		temp = document.getElementById(elementID).value;
		if (temp.length < output.length) {  // backspace
			output = temp;
		} else if (temp.charAt(temp.length - 1) == "`") { // switch lang if I press ` key
			output = temp.substring(0, temp.length - 1);
			document.getElementById(elementID).value = output;
			this.switchLang(); // Called switchLang
		} else if (temp.length > output.length) {
			st = "";
			str = temp.charAt(temp.length - 1);
			if (this.langState == 1) {
				ii = this.CharToCode(str);
				this.oldState = this.capsState;
				this.capsState = this.shiftState;
				st = this.EngToKhmer(ii);
				this.capsState = this.oldState;
				temp = temp.substring(0, temp.length - 1);
			}
			output = temp + st;	
			document.getElementById(elementID).value = output;
		}
		
	}; // end proKey()


	/**
	 * this method allow you to override when you need to integrate
	 */
	this.switchLang = function () {
		if (this.langState == 0) {
			//document.ADDFORM.langstatus.value = "Khmer";
			this.langState = 1;
		} else {
			//document.ADDFORM.langstatus.value = "English";
			this.langState = 0;
		}
	}; // end switchLang()
	
	this.switchLang1 = function () {
		if (this.langState == 0) {
			document.ADDFORM.langstatus.value = "Khmer";
			this.langState = 1;
		} else {
			document.ADDFORM.langstatus.value = "English";
			this.langState = 0;
		}
	}; // end switchLang()	

}; // end of class

//Create an instance 
km = new KhmerKey();